#include "include/planesystem2.h"
// 当发生byte不确定时，只需将.h文件导入引用的.cpp文件即可
// connect 函数应放在发出者创建语句的函数体内

// 构造函数
PlaneSystem2::PlaneSystem2(QWidget *parent)
    : QMainWindow(parent)
    ,ui(new Ui::PlaneSystem2Class)
    ,isLoggedIn(false)
    ,login(nullptr)
    ,currentClient(new Client)
{
    ui->setupUi(this);
    InitWindows();
    InitTicketBookWindow();
    LoginButton();
    InitSidePanel();
    MoreFunButton();
    InitMoreFunSidePanel();
}

// 析构函数
PlaneSystem2::~PlaneSystem2()
{
    delete ui;
}


// 点击登录按钮槽函数
void PlaneSystem2::onLoginButtonClicked() {
    // 开始登录
    if (isLoggedIn) {
        // 如果已经登录，显示侧边面板
        sidePanel->show();
        sidePanel->move(WindowWidth - sidePanel->width(), 0); // 将其移动到右侧
    }
    else {
        // 如果尚未登录，检查是否已经有登录对话框存在且不可见
        if (login == nullptr || !login->isVisible()) {
            login = new LoginDialog(this, &currentClient); // 如果没有，则创建登录对话框  // 更新登录信息
            login->show(); // 显示登录对话框
            // connect 函数应放在发出者创建语句的函数体内
            connect(login, &LoginDialog::loginSuccess, this, &PlaneSystem2::updateLoginStatus);
        }
    }

}

// 在主屏幕的 mousePressEvent 中重载并发出信号
void PlaneSystem2::mousePressEvent(QMouseEvent* event) {
    if (event->button() == Qt::LeftButton) {
        emit mainScreenClicked(); // 发出自定义信号
    }
    QMainWindow::mousePressEvent(event);
}

// 在 PlaneSystem2 类中重写事件过滤器
bool PlaneSystem2::eventFilter(QObject* obj, QEvent* event) {
    if ((obj == sidePanel||obj== moreFunsidePanel) && (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonDblClick)) {
        // 阻止事件穿透，不让它传递到下面的控件
        return true; // 返回 true 表示事件已处理
    }
    return QMainWindow::eventFilter(obj, event);
}

// 槽函数：点击主屏幕隐藏侧屏
void PlaneSystem2::onMainWindowClicked() {
    sidePanel->hide();
    moreFunsidePanel->hide();
}


// 以后功能拓展（用户上传头像
void PlaneSystem2::onAvatarButtonClicked() {
    
}

// 更新登录状态
void PlaneSystem2::updateLoginStatus() {
    if (currentClient->getClientId() != 0 && !isLoggedIn) {
        // 更改登录标志
        isLoggedIn = true;

        // 登录按钮
        loginButton->setText("");
        loginButton->setIcon(QIcon("./res/client/default.png"));
        loginButton->setIconSize(QSize(50, 50));

        // 面板信息
        avatarButton->setIcon(QIcon("./res/client/default.png"));
        avatarButton->setIconSize(QSize(50, 50));
        clientNameLabel->setText(QString::fromStdString(currentClient->getClientName()));
    }
}

QPushButton* PlaneSystem2::getLoginButton() {
    return loginButton;
}

// 槽函数：更多功能按钮
void PlaneSystem2::onMoreFunButtonClicked() {
    moreFunsidePanel->show();
    moreFunsidePanel->move(0, 74); // 将其移动到左侧
}

// 槽函数：点击个人信息后
void PlaneSystem2::onClientMegButtonClicked() {
    if (clientMegPanel == nullptr || !clientMegPanel->isVisible()) {
        printf("查看个人信息！\n");
        this->InitClientMegButtonPanel();
    }
}

// 槽函数：点击按钮关闭
void PlaneSystem2::onClientMegButtonCloseClicked() {
    clientMegPanel->close();
}


// 槽函数：点击航班按钮后
void PlaneSystem2::onTicketMsgButtonClick() {
    if (ticketMegPanel == nullptr || !ticketMegPanel->isVisible()) {
        // 获取当前航班的具体信息
        QPushButton* btn = qobject_cast<QPushButton*>(sender());
        Trip tripInfo = btn->property("trip").value<Trip>();
        // 生成面板
        this->InitTicketMegButtonPanel(tripInfo);
    }
}

// 槽函数：点击按钮关闭
void PlaneSystem2::onTicketMegButtonCloseClicked() {
    ticketMegPanel->close();
}

// 槽函数：点击预订按钮
void PlaneSystem2::onBookButtonClicked() {
    QPushButton* btn = qobject_cast<QPushButton*>(sender());
    int tripId = btn->property("tripId").value<int>();

    // 检测该航班是否已经被客户预订

    if (currentClient->getClientId() != 0) {
        if (currentClient->IsBookTripTicket(tripId)) {
            if (currentClient->BookTripTicket(tripId)) {
                // 预定成功
                QMessageBox::information(this, "信息", "预订成功！");
                ticketMegPanel->close();  
            }
        }
        else {
            //不可重复预订
            QMessageBox::information(this, "信息", "不可重复预订！");
            ticketMegPanel->close();
        }
    }
    else {
        // 需要登录
        QMessageBox::information(this, "信息", "请先登录账号！");
    }
}

// 槽函数：个人信息-预订机票按钮
void PlaneSystem2::onBookingButtonClicked() {
    if (bookTripPanel == nullptr || !bookTripPanel->isVisible()) {
        printf("查看订票信息！\n");
        InitClientMegBookingTripButtonPanel();
    }
}

// 点击按钮关闭
void PlaneSystem2::onClientMegBookingTripButtonClicked() {
    bookTripPanel->close();
}

void PlaneSystem2::onBookTicketMsgButtonClick() {
    if (bookTicketMegPanel == nullptr || !bookTicketMegPanel->isVisible()) {
        // 获取当前航班的具体信息
        QPushButton* btn = qobject_cast<QPushButton*>(sender());
        Trip tripInfo = btn->property("trip").value<Trip>();
        string bookTime = btn->property("time").value<string>();
        // 生成面板
        this->InitBookTicketMegButtonPanel(tripInfo, bookTime);
    }
}

void PlaneSystem2::onBookTicketMegButtonCloseClicked() {
    bookTicketMegPanel->close();
}

// 点击退订按钮
void PlaneSystem2::onRefundButtonClicked() {
    QPushButton* btn = qobject_cast<QPushButton*>(sender());
    int tripId = btn->property("tripId").value<int>();

    if (currentClient->RefundTripTicket(tripId)) {
        // 退订成功
        QMessageBox::information(this, "信息", "退订成功！");
        bookTicketMegPanel->close();
    }
}